//
//  SISSComboBoxViewProgress.m
//  SISSAPP
//
//  Created by 优房网 on 2017/4/18.
//  Copyright © 2017年 优房网. All rights reserved.
//

#import "SISSComboBoxViewProgress.h"

#import "SISSAreaModel.h"

@interface SISSComboBoxViewProgress ()<MMComBoBoxViewDataSource, MMComBoBoxViewDelegate>

@property (nonatomic, strong) NSArray *mutableArray;

/** 判断房源|客源 */
@property (assign, nonatomic) BOOL isHouseResource;

/** 是否是租赁 */
@property (assign, nonatomic) BOOL isRent;

@end
@implementation SISSComboBoxViewProgress

- (instancetype)initWithFrame:(CGRect)frame isHouseResource:(BOOL)isHouseResource isRent:(BOOL)isRent {
    
    self.isHouseResource = isHouseResource;
    self.isRent = isRent;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame isHouseResource:(BOOL)isHouseResource {
    self.isHouseResource = isHouseResource;
    
    return [self initWithFrame:frame];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    self.comBoBoxView = [[MMComBoBoxView alloc] initWithFrame:self.bounds];
    self.comBoBoxView.dataSource = self;
    self.comBoBoxView.delegate = self;
    [self addSubview:self.comBoBoxView];
    [self.comBoBoxView reload];
}



#pragma mark - MMComBoBoxViewDataSource
- (NSUInteger)numberOfColumnsIncomBoBoxView :(MMComBoBoxView *)comBoBoxView {
    return self.mutableArray.count;
}
- (MMItem *)comBoBoxView:(MMComBoBoxView *)comBoBoxView infomationForColumn:(NSUInteger)column {
    return self.mutableArray[column];
}

#pragma mark - MMComBoBoxViewDelegate
- (void)comBoBoxView:(MMComBoBoxView *)comBoBoxViewd didSelectedItemsPackagingInArray:(NSArray *)array atIndex:(NSUInteger)index {
    MMItem *rootItem = self.mutableArray[index];
    switch (rootItem.displayType) {
        case MMPopupViewDisplayTypeNormal: {
            //拼接选择项
            NSMutableString *title = [NSMutableString string];
            __block NSInteger firstPath;
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                [title appendString:idx?[NSString stringWithFormat:@";%@",[rootItem findTitleBySelectedPath:path]]:[rootItem findTitleBySelectedPath:path]];
                if (idx == 0) {
                    firstPath = path.firstPath;
                }
            }];
            
            NSLog(@"当title为%@时，所选字段为 %@",rootItem.title ,title);
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:title:)]) {
                
                [self.delegate comboBoxViewDidSelectDisplayTypeNormalWithRootItemTitle:rootItem.title title:title];
            }
            
            break;
        }
            
        case MMPopupViewDisplayTypeMultilayer: {
            
            [array enumerateObjectsUsingBlock:^(MMSelectedPath * path, NSUInteger idx, BOOL * _Nonnull stop) {
                
                // 拿到地区的前一个
                MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
//                MMItem *thirdItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath].childrenNodes[path.thirdPath];
//                NSLog(@" rootItem.title:%@  当firstItem.title为%@时，所选字段secondItem.title为 %@,--thirdItem.title %@",rootItem.title,firstItem.title,secondItem.title,thirdItem.title);
                NSLog(@" rootItem.title:%@  当firstItem.title为%@时，所选字段secondItem.title为 %@",rootItem.title,firstItem.title,secondItem.title);
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:title:subtitle:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultilayerWithRootItemTitle:rootItem.title title:firstItem.title subtitle:secondItem.title];
                }
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:firstTitle:secondTitle:thirdTitle:)]) {
                    [self.delegate comboBoxViewDidSelectDisplayTypeMultiThreelayersWithRootItemTitle:rootItem.title firstTitle:firstItem.title secondTitle:secondItem.title thirdTitle:secondItem.title];
                }
            }];
            
            break;
        }
        case MMPopupViewDisplayTypeFilters:{
            MMCombinationItem *combinationItem = self.mutableArray[index];
            [array enumerateObjectsUsingBlock:^(NSMutableArray*  _Nonnull subArray, NSUInteger idx, BOOL * _Nonnull stop) {
                if (combinationItem.isHasSwitch && idx == 0) {
                    for (MMSelectedPath *path in subArray) {
                        MMAlternativeItem *alternativeItem = combinationItem.alternativeArray[path.firstPath];
                        NSLog(@"当title为: %@ 时，选中状态为: %d",alternativeItem.title,alternativeItem.isSelected);
                        
                    }
                    return;
                }
                
                NSString *title;
                NSMutableArray *subtitles = [NSMutableArray array];
                for (MMSelectedPath *path in subArray) {
                    MMItem *firstItem = rootItem.childrenNodes[path.firstPath];
                    MMItem *secondItem = rootItem.childrenNodes[path.firstPath].childrenNodes[path.secondPath];
                    title = firstItem.title;
                    if ([title isEqualToString:@"进度"]) {
                        [subtitles addObject:[NSString stringWithFormat:@"%zd",secondItem.tag + 2]];
                    } else {
                        [subtitles addObject:secondItem.title];
                    }
                    //                    [subtitles appendString:[NSString stringWithFormat:@"  %@",secondItem.title]];
                }
                NSLog(@"当title为%@时，所选字段为 %@",title,subtitles);
                if ([self.delegate respondsToSelector:@selector(comboBoxViewDidSelectDisplayTypeFiltersWithTitle:subtitles:)]) {
                    
                    [self.delegate comboBoxViewDidSelectDisplayTypeFiltersWithTitle:title subtitles:subtitles.copy];
                }
            }];
            
            if ([self.delegate respondsToSelector:@selector(comboBoxViewDisplayTypeFiltersConfirmClickAction)]) {
                [self.delegate comboBoxViewDisplayTypeFiltersConfirmClickAction];
            }
            
            break;}
        default:
            break;
    }
}



- (NSArray *)mutableArray {
    
    
    if (_mutableArray == nil) {
        NSMutableArray *mutableArray = [NSMutableArray array];
        NSArray<SISSAreaModel *> *areaArr = [SISSAreaModel loadAreaData];
        
        NSString *areaTitleName = self.isHouseResource ? @"区域" : @"意向区域";
        // 区域
        MMMultiItem *rootItemArea = [MMMultiItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaTitleName];
        rootItemArea.displayType = MMPopupViewDisplayTypeMultilayer;
        for (int i = 0; i < areaArr.count; i++){
            
            SISSAreaModel *areaModel = areaArr[i];
            
            MMItem *rootItemArea_A = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaModel.name];
            rootItemArea_A.isSelected = (i == 0);
            [rootItemArea addNode:rootItemArea_A];
            for (int j = 0; j < areaModel.areas.count; j ++) {
                
                MMItem *rootItemArea_B = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:areaModel.areas[j]];
                rootItemArea_B.isSelected = (j == 0);
                [rootItemArea_A addNode:rootItemArea_B];
            }
        }
        
        // 售价
        NSString *priceTitleName = self.isRent ? (self.isHouseResource ? @"租金" : @"租金预算") : (self.isHouseResource ? @"售价" : @"预算");
        NSArray *priceArr = @[@"不限", @"200万以下", @"200-300万", @"300-500万", @"500-800万", @"800-1200万", @"1200万-2000万", @"2000万以上"];
        priceArr = self.isRent ? @[@"不限", @"500元以下", @"500-1000元", @"1000-2000元", @"2000-3000元", @"3000-5000元", @"5000-8000元", @"8000-11000元", @"11000元以上"] : priceArr;
        MMSingleItem *rootItemPrice = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:priceTitleName];
        for (int i = 0; i < priceArr.count; i++) {
            [rootItemPrice addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:priceArr[i]]];
        }
        
        // 户型
        NSArray *houseTypeArr = @[@"不限", @"一房", @"二房", @"三房", @"四房", @"四房以上", @"复式", @"别墅", @"其他"];
        MMSingleItem *rootItemHouseType = [MMSingleItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"户型"];
        //        rootItemHouseType.selectedType = MMPopupViewMultilSeMultiSelection;
        for (int i = 0; i < houseTypeArr.count; i++) {
            [rootItemHouseType addNode:[MMItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:houseTypeArr[i]]];
        }
        
        
        // 更多
        MMCombinationItem *rootItemMore = [MMCombinationItem itemWithItemType:MMPopupViewDisplayTypeSelected titleName:@"更多"];
        rootItemMore.displayType = MMPopupViewDisplayTypeFilters;
        rootItemMore.selectedType = MMPopupViewMultilSeMultiSelection;
        
        NSArray *progressTagArr = self.isHouseResource ? @[@"签署委托合同", @"房屋状况说明书", @"实地看房", @"签署卖家告知书" ,@"签署买家告知书", @"签署居间合同", @"服务评价"] : @[@"签署委托合同", @"实地看房", @"签署买家告知书" ,@"签署卖家告知书", @"签署居间合同", @"服务评价"];
        
        NSArray *moreArr = @[
                          @{@"委托方式":@[@"定向委托", @"公开委托"]},
                          @{@"委托次数":@[@"1~3次", @"3~7次", @"7~15次", @"15次以上"]},
                          @{@"进度":progressTagArr},
                          @{@"面积":@[@"50m²以下", @"50-70m²", @"70-90m²", @"90-120m²", @"120-150m²", @"150-200m²", @"200-300m²", @"300m²以上"]},
                          @{@"朝向":@[@"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西南", @"西北"]},
                          @{@"装修程度":@[@"毛坯", @"普装", @"精装", @"豪装"]}
                          ];
        NSArray *progressTagArr2 = self.isHouseResource ? @[@"签署委托合同", @"房屋状况说明书", @"实地看房", @"签署租赁及经纪合同", @"服务评价"] : @[@"签署委托合同", @"实地看房", @"签署租赁及经纪合同", @"服务评价"];
        NSArray *arr = @[
                         @{@"委托次数":@[@"1~3次", @"3~7次", @"7~15次", @"15次以上"]},
                         @{@"进度":progressTagArr2},
                         @{@"面积":@[@"50m²以下", @"50-70m²", @"70-90m²", @"90-120m²", @"120-150m²", @"150-200m²", @"200-300m²", @"300m²以上"]},
                         @{@"朝向":@[@"朝东", @"朝南", @"朝西", @"朝北", @"东南", @"东北", @"西南", @"西北"]},
                         @{@"装修程度":@[@"毛坯", @"普装", @"精装", @"豪装"]}
                         ];
        moreArr = self.isRent ? arr : moreArr;
        for (NSDictionary *itemDic in moreArr) {
            MMItem *itemMore_A = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:[itemDic.allKeys lastObject]];
            [rootItemMore addNode:itemMore_A];
            for (int i = 0; i <  [[itemDic.allValues lastObject] count]; i++) {
                NSString *title = [itemDic.allValues lastObject][i];
                MMItem *itemMore_B = [MMItem itemWithItemType:MMPopupViewDisplayTypeUnselected titleName:title];
                //                if (i == 0) {
                //                    item4_B.isSelected = YES;
                //                }
                itemMore_B.tag = i;
                [itemMore_A addNode:itemMore_B];
            }
        }
        
        [mutableArray addObject:rootItemArea];
        [mutableArray addObject:rootItemPrice];
        [mutableArray addObject:rootItemHouseType];
        [mutableArray addObject:rootItemMore];
        _mutableArray  = [mutableArray copy];
    }
    return _mutableArray;
}


@end
